
package com.jf.cloud.multishop.controller.multishop;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.AuditStatus;
import com.jf.cloud.multishop.dto.ShopCompanyDTO;
import com.jf.cloud.multishop.model.ShopCompany;
import com.jf.cloud.multishop.service.ShopAuditingService;
import com.jf.cloud.multishop.service.ShopCompanyService;
import com.jf.cloud.multishop.vo.ShopCompanyVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zz
 * @Date 2021/4/25 16:31
 */
@RestController
@RequestMapping("/m/apply_shop/shop_company")
@Tag(name = "multishop-店铺工商信息")
public class ShopCompanyController {

    @Autowired
    private ShopCompanyService shopCompanyService;
    @Autowired
    private ShopAuditingService shopAuditingService;
    @Autowired
    private MapperFacade mapperFacade;

    @PostMapping
    @Operation(summary = "新增店铺工商信息" , description = "新增店铺工商信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ShopCompanyDTO shopCompanyDTO) {
        Long shopId = this.getShopId();
        ShopCompany shopCompany = mapperFacade.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setShopId(shopId);
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        shopCompanyService.save(shopCompany, true);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "商家端更新店铺工商信息,仅在申请开店时调用" , description = "更新店铺工商信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopCompanyDTO shopCompanyDTO) {
        ShopCompany shopCompany = mapperFacade.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        shopCompanyService.checkInfo(shopCompany, false);
        shopCompanyService.update(shopCompany);
        return ServerResponseEntity.success();
    }

    @PostMapping("/storage")
    @Operation(summary = "存储店铺工商信息" , description = "存储店铺工商信息,已存在则更新，不存在则新增")
    public ServerResponseEntity<Void> storage(@Valid @RequestBody ShopCompanyDTO shopCompanyDTO) {
        Long shopId = this.getShopId();
        ShopCompanyVO shopCompanyVO = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.WAITAUDIT.value());
        ShopCompany shopCompany = mapperFacade.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setShopId(shopId);
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(shopCompanyVO)) {
            // 不存在，新增
            shopCompanyService.save(shopCompany, true);
        } else {
            // 已存在，更新
            shopCompany.setShopCompanyId(shopCompanyVO.getShopCompanyId());
            shopCompanyService.checkInfo(shopCompany, false);
            shopCompanyService.update(shopCompany);
        }
        return ServerResponseEntity.success();
    }

    @GetMapping
    @Operation(summary = "获取店铺工商信息")
    @Parameter(name = "status", description = "审核状态：1：已通过 0待审核 -1未通过" )
    public ServerResponseEntity<ShopCompanyVO> getShopCompanyByShopId(@ParameterObject @RequestParam(value = "status",required = false, defaultValue = "1") Integer status) {
        Long shopId = this.getShopId();
        Integer isPassShop = AuthUserContext.get().getIsPassShop();
        if (Objects.equals(isPassShop, StatusEnum.DISABLE.value())) {
            status = AuditStatus.WAITAUDIT.value();
        }
        return ServerResponseEntity.success(shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, status));
    }

    private Long getShopId() {
        Long shopId = AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.DEFAULT_SHOP_ID)) {
            throw new LuckException("店铺不存在");
        }
        return shopId;
    }

}
